package com.anjie.powerproject.service.impl;

import com.anjie.powerproject.entity.EnergyType;
import com.anjie.powerproject.entity.Relationship;
import com.anjie.powerproject.repository.EnergyTypeRepository;
import com.anjie.powerproject.repository.RelationshipRepository;
import com.anjie.powerproject.service.EnergyTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EnergyTypeServiceImpl implements EnergyTypeService {

    @Autowired
    private EnergyTypeRepository energyTypeRepository;

    @Autowired
    private RelationshipRepository relationshipRepository;

    @Override
    public List<EnergyType> getAllEnergyType() {
        return energyTypeRepository.findAll();
    }

    @Override
    public EnergyType addOneEnergyType(EnergyType energyType) {
        return energyTypeRepository.saveAndFlush(energyType);
    }

    @Override
    public EnergyType findEnergyTypeById(Integer id) {
        return energyTypeRepository.findById(id).orElse(null);
    }

    @Override
    public EnergyType saveAndFlush(EnergyType energyType) {
        return energyTypeRepository.save(energyType);
    }

    @Override
    public boolean checkEnergyTypeName(String energyTypeName) {
        List<EnergyType> equals = energyTypeRepository.findEnergyTypesByTypeNameEquals(energyTypeName);
        if (equals==null||equals.size()==0)
            return true;
        else
            return false;
    }

    @Override
    public boolean deleteEnergyType(Integer typeId) {
        List<Relationship> list = relationshipRepository.findRelationshipsByTypeId(typeId);
        if (list.size()>0){
            return false;
        }else {
            energyTypeRepository.deleteById(typeId);
            return true;
        }
    }
}
